/**
* \file test.c
* \brief Programme de test GeoIP
* \author A. Morel
* \version 0.1
* \date 28 april 2010
*
* Programme de test pour GeoIP
*
*/
#include "test.h"

/**
* \fn Fonction main principale
* \brief On test les paramètres passés, puis on exécute les fonctions appelées
*
* \param argv chaine argument
* \return Valeur EXIT_SUCCESS ou EXIT_FAILURE
*/
int main (int argc, char *argv[]) {

  int optch;
  extern int opterr;
  char * host;
  char * buff;

  char format[] = "hc:"; 
  opterr = 1;

  int country_code=0;

  while((optch = getopt(argc, argv, format)) != -1)
    switch(optch) {
      case 'c':
        country_code = 1;
        host = optarg;
        break;
      case 'h':
        fprintf(stderr,"Utilisation\n");
        fprintf(stderr,"%s [h|c HOST]\n",argv[0]);
        fprintf(stderr,"-c HOST:\tRetour the coutry code of the HOST\n");
    }

  GeoIP * gi;
  gi = GeoIP_new(GEOIP_STANDARD);
  if (!gi) {
    fprintf(stderr, "Cannot initialize GeoIP !\n");
    return(EXIT_FAILURE);
  }
  if (country_code == 1 ) {
      if ((buff = (char * )GeoIP_country_code_by_name(gi, host)) != NULL) {
        printf("Country code: %s\n", buff);
      } else {
        printf("Undefined host\n");
      }
  }
}

